package io.renren.modules.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import io.renren.modules.sys.dao.BusinessDao;
import io.renren.modules.sys.entity.BusinessEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.entity.SysRoleEntity;
import io.renren.modules.sys.service.BusinessService;
import io.renren.modules.sys.service.SysRoleService;
import io.renren.modules.sys.dao.SysUserRoleDao;
import io.renren.modules.sys.dao.SysUserDao;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;


@Service("businessService")
public class BusinessServiceImpl extends ServiceImpl<BusinessDao, BusinessEntity> implements BusinessService {

    @Autowired
    private SysUserRoleDao sysUserRoleDao;

    @Autowired
    private SysUserDao sysUserDao;

    @Autowired
    private SysRoleService sysRoleService;

    @Override
    public BusinessEntity queryList() {
        BusinessEntity businessEntity = new BusinessEntity();
        SysUserEntity user = (SysUserEntity)SecurityUtils.getSubject().getPrincipal();

        Long roleId = sysUserRoleDao.queryRoleId(user.getUserId());
        int todayorder = 0;
        int yesterdayorder = 0;
        int totalorder = 0;
        BigDecimal todaytrade = new BigDecimal(0);
        BigDecimal yesterdaytrad = new BigDecimal(0);
        BigDecimal totaltrad = new BigDecimal(0);
        if(roleId!=null){
            SysRoleEntity role = sysRoleService.getById(roleId);
            String shopid = "";
            if(role.getRoleName().indexOf("管理员")!=-1){  //当前登陆用户为平台，展示所有的订单信息

            }
            if(role.getRoleName().indexOf("加盟商")!=-1||role.getRoleName().indexOf("渠道")!=-1){//当前登陆用户为渠道(加盟商)，展示渠道下所有店铺订单
                List<Long> userlist = sysUserDao.queryChildUser(user.getUserId());
                if(userlist!=null && userlist.size()>0){
                    for(int i=0;i<userlist.size();i++){
                        shopid +=userlist.get(i);
                        if(i!=userlist.size()-1){
                            shopid +=",";
                        }
                    }
                }
            }
            if(role.getRoleName().indexOf("店铺")!=-1){//当前登陆用户为店铺，展示店铺下所有店铺订单
                shopid = user.getUserId()+"";

            }
            todayorder = baseMapper.queryTodayOrderNum(shopid); //今日订单
            yesterdayorder = baseMapper.queryYesterdayOrderNum(shopid);//昨日订单
            totalorder = baseMapper.queryTotalOrderNum(shopid);//总订单
            todaytrade = baseMapper.queryTodayTradeVolume(shopid);//今日交易额
            yesterdaytrad = baseMapper.queryYesterdayTradeVolume(shopid);//昨日交易额
            totaltrad = baseMapper.queryTotalTradeVolume(shopid);//总交易额
        }

        businessEntity.setTodayOrderNum(todayorder);
        businessEntity.setYesterdayOrderNum(yesterdayorder);
        businessEntity.setTotalOrderNum(totalorder);
        businessEntity.setTodayTradeVolume(todaytrade == null?new BigDecimal(0):todaytrade);
        businessEntity.setYesterdayTradeVolume(yesterdaytrad == null?new BigDecimal(0):yesterdaytrad);
        businessEntity.setTotalTradeVolume(totaltrad == null?new BigDecimal(0):totaltrad);
        return businessEntity;
    }
}
